﻿var regionId;//地区ID
var schoolname;//学校名称
var category;//学校类型

var maxentries = 0;
var options = { items_per_page: 20, link_to: "javascript:;", callback: pageChanged }
$(document).ready(function() {

    $("#dv_noresult").css("display", "none");

    getValue();
    BindSearchSwitch();
    searchSchools();

    //获得选择的地区
    $("#txt_region").regions({ dataurl: rootpath + "api/Region.ashx", parmsdata: { type: "getCity", pid: 0} });

    //搜索学校
    $("#btn_search").click(function() {
        getValue();
        searchSchools();
    });

    //删除学校
    $("#btn_delete").click(function() {
        bindMulDeleteEvent();
    });
    
    showDialog();
});   

function searchSchools() {
    $.ajax({
        type: "GET",
        url: "../api/School.ashx",
        data: { type: 'schoolsum', regionId: regionId, schoolname: schoolname, category: category, r: Math.random() },
        dataType: "html",
        success: function(data) {
            if (data > 0) {

                $("#dv_noresult").css("display", "none");
                
                maxentries = data;
                $("#pager").pagination(maxentries, options);
            } else {
            
                $("#dv_noresult").css("display", "block");
                $("#dv_result").css("display", "none");
            }
        }
    });
}

function pageChanged(index, jq) {
    $.ajax({
        type: "GET",
        url: "../api/School.ashx",
        data: { type: 'searchschool', regionId: regionId, schoolname: schoolname, category: category, pageindex: index + 1, pagesize: options.items_per_page, r: Math.random() },
        dataType: "json",
        success: function(data) {
            $(".DG tbody").empty();
            $.each(data, function(i, item) {
                $(".DG tbody").append("<tr><td><input type='checkbox' value='" + item.schoolId + "' /></td>\
                                            <td>" + item.row + "</td>\
                                            <td>" + item.schoolName + "</td>\
                                            <td>" + item.address + "</td>\
                                            <td>" + item.description + "</td>\
                                            <td>\
                                                <input type=\"button\" class=\"editButton\" id=\"btn_editschool_" + item.schoolId + "\" title=\"编辑\"/>\
                                                <input type=\"button\" class=\"addbutton\" id=\"btn_addclasses_" + item.schoolId + "\" title=\"添加班级\"/>\
                                            </td>\
                                       </tr>");

                //编辑学校
                $("#btn_editschool_" + item.schoolId + "").click(function() {
                    setValue(item);
                    $("#dv_editschool").dialog("open");
                    return false;

                });

                //添加班级
                $("#btn_addclasses_" + item.schoolId + "").click(function() {
                    window.location.href = "../classes/addclass.aspx?schoolid=" + item.schoolId;
                });
            });
            BindCheckBoxEvent($("#btn_delete"));
            $(".DG tbody tr").hover(function() { $(this).addClass("tdmover"); }, function() { $(this).removeClass("tdmover"); });
            formReady();
        }
    });
}

function editSchool() {

    var id = $("#hid_schoolid").val();
    var name = $.trim($("#txt_name").val());
    var addr = $.trim($("#txt_adress").val());
    var des = $("#txt_desc").val();
    var cate = $("#ddl_category option:selected").val();

    if (name == "") {
        removeCss();
        addSpan("red", "请输入学校名称！", "#txt_name");
    } else if (addr == "") {
        removeCss();
        addSpan("red", "请输入学校地址！", "#txt_adress");
    } else {
        $.ajax({
            type: "POST",
            url: "../api/School.ashx",
            data: { type: 'editschool', schoolid: id, schoolname: name, address: addr, description: des, cate: cate, r: Math.random() },
            dataType: "html",
            success: function(data) {
                if (data == 1) {
                    searchSchools();
                    $("#dv_editschool").dialog("close");
                }
            }
        });
    }
}

function bindMulDeleteEvent() {
    confirmbox("确定要删除选中的学校吗？");

    $(window.parent.document).find("#btn_sure").click(function() {
        closebox();
        var sids = "";

        $("input:checked").not("#cb_All").each(function() {
            sids += $(this).val() + ",";
        });

        doDelete(sids);
    });
}

function doDelete(sids) {
    $.ajax({
        type: "POST",
        url: "../api/School.ashx",
        data: { type: 'deleteSchool', sids: sids, r: Math.random() },
        dataType: "html",
        success: function(data) {
            if (data == 1) {
                searchSchools();
            } 
        } 
    }); 
}

function showDialog() {
    $("#dv_editschool").dialog({
        autoOpen: false,
        width: 450,
        height: 300,
        modal: true,
        title: "编辑学校",
        buttons: {
            "取消": function() { $(this).dialog("close"); },
            "确定": function() { editSchool(); }
        },
        resizable: false
    });
}

function setValue(item) {
    removeCss();
    $("#hid_schoolid").attr("value", item.schoolId);
    $("#txt_name").attr("value", item.schoolName);
    $("#txt_adress").attr("value", item.address);
    $("#txt_desc").attr("value", item.description);
    $("#ddl_category > option").each(function() {
        if ($(this).val() == item.category) {
            $(this).attr("selected", "selected");
        }
    });    
}

function getValue() {
    regionId = $("#txt_region").attr("rel");
    schoolname = $.trim($("#txt_schoolname").val());
    category = $("#sel_category option:selected").val();
}

function removeCss() {
    $("#txt_adress").next("span").remove();
    $("#txt_name").next("span").remove();
}

function addSpan(color, msg, clientId) {
    $("<span/>").css("color", color).html(msg).insertAfter(clientId);
}



